﻿using System;
using HKEA.CMS.Core.DALFactory;
using HKEA.CMS.Core.IDAL;
using HKEA.CMS.Core.Models;

namespace HKEA.CMS.Core.BLL
{
    public class tbCodeGeneratorBLL
    {
        private  ItbCodeGeneratorDAL IDAL=DataAccess.CreateICodeBuilder();
        private static object obj = new Object();
        private static tbCodeGeneratorBLL bll = null;

        #region 构造函数相关

        /// <summary>
        /// 构造函数
        /// </summary>
        public tbCodeGeneratorBLL()
        {
        }

        /// <summary>
        /// 创建对象
        /// </summary>
        /// <returns></returns>
        public static tbCodeGeneratorBLL GetInstance()
        {
            if (bll == null)
                lock (obj)
                    if (bll == null)
                        bll = new tbCodeGeneratorBLL();
            return bll;
        }

        #endregion

        #region 对外方法

        /// <summary>
        /// 生成会员的编码 ，DateTime只需要年份和月份 部分即可
        /// </summary>
        /// <param name="type">生成的编码类型</param>
        /// <returns>编码字符串</returns>
        public string BuilderCustomerCode(string type,DateTime dt)
        {
            string ret = string.Empty;
            tbCodeGenerator ent = GetEntByType(type);
            int year = dt.Year; int month = dt.Month;
            if (ent != null)
            {
                if (ent.year == year && ent.month == month)
                    ent.real_index += 1;
                else
                {
                    ent.year = year;
                    ent.month = month;
                    ent.real_index = 1;                    
                }
                ent.virtual_index = ent.real_index.ToString().PadLeft(5, '0');
                UpdateENT(ent);
                ret = string.Format("{0}{1}{2}{3}",  year, month.ToString().PadLeft(2,'0'),type, ent.virtual_index);
            }
            else
            {
                ent = new tbCodeGenerator();
                ent.type = type;
                ent.year = year;
                ent.month = month;
                ent.real_index = 1;
                ent.virtual_index = ent.real_index.ToString().PadLeft(5, '0');
                InsertENT(ent);
                ret = string.Format("{0}{1}{2}{3}",  year, month.ToString().PadLeft(2,'0'),type, ent.virtual_index); ;
            }
            return ret;
        }

        /// <summary>
        /// 生成通用的编码 除了会员的编码之外（会员的编码生成禁用此函数）
        /// </summary>
        /// <param name="type">生成的编码类型</param>
        /// <returns>编码字符串</returns>
        public string BuilderGeneralCode(string type)
        {
            string ret = string.Empty;
            tbCodeGenerator ent = GetEntByType(type);
            if (ent != null)
            {
                if (ent.year <= 0)
                {
                    ent.real_index += 1;
                    ent.virtual_index = ent.real_index.ToString().PadLeft(5, '0');
                    UpdateENT(ent);
                    ret = string.Format("{0}_{1}",type,ent.virtual_index);
                }
            }
            else
            {
                ent = new tbCodeGenerator();
                ent.type = type;
                ent.real_index += 1;
                ent.virtual_index = ent.real_index.ToString().PadLeft(5, '0');
                UpdateENT(ent);
                ret = string.Format("{0}_{1}", type, ent.virtual_index);
            }
            return ret;
        }

        #endregion

        #region 内部方法

        protected bool IsExist(string type)
        {
            return IDAL.IsExist(type);
        }

        protected bool IsExist(string type, int year, int month)
        {
            return IDAL.IsExist(type,year,month);
        }

        protected tbCodeGenerator GetEntByType(string type)
        {
            return IDAL.GetEntByType(type);
        }

        protected int InsertENT(tbCodeGenerator ent)
        {
            return IDAL.InsertENT(ent);
        }

        protected int UpdateENT(tbCodeGenerator ent)
        {
            return IDAL.UpdateENT(ent);
        }

        #endregion
    }
}
